import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
	state:{
		order:{car:{},enterprise:{},details:{},isInsurance:0,contract:{},amount:0},
		maintenanceOrder:{byClass:{},byItem:'',byItemNum:0,totalPrice:0,planType:'',discount:''},
		carMileage:JSON.parse(localStorage.getItem('carMileage')),
		mycar:JSON.parse(localStorage.getItem('mycar')),
		enterpriseFlag: localStorage.getItem('enterpriseFlag')=='true' ? true: false,
		profile:JSON.parse(localStorage.getItem('profile')),
	},
	mutations:{
		//更新我的车辆信息
		UPD_MY_CAR(state,data) {
			state.mycar=data;
            localStorage.setItem('mycar',JSON.stringify(data));
		},
		//选择订单的服务单位
		SELECT_ENTERPRISE(state,enterprise) {
			state.order.enterprise=enterprise;
		},
		//更新订单车辆信息
		UPD_ORDER_CAR(state,car) {
            state.order.car=car;
        },
        //更新订单联系人信息
        UPD_ORDER_CONTRACT(state,contract) {
            state.order.contract=contract;
        },
        //更新订单购物车信息
        UPD_ORDER_DETAILS(state,details) {
            state.order.details=details;
        },
		UPD_ORDER_INSURANCE(state,flag) {
            state.order.isInsurance=flag;
		},
		//初始化订单车辆信息
		INIT_ORDER_CAR(state) {
			if (state.order.car.carPlateNumber == undefined) {
				state.order.car = state.mycar;
			}
		},
        UPD_PROFILE_PHONE(state,phone) {
            state.profile.peoplePhone = phone;
			localStorage.setItem('profile',JSON.stringify(state.profile));
        },
		//选择保养项目
		UPD_MAINTENANCE_ITEM(state,data){
			state.maintenanceOrder.byItem = data;
			localStorage.removeItem('maintenanceOrder');
			localStorage.setItem('maintenanceOrder',JSON.stringify(state.maintenanceOrder));
		},
		//更新车辆里程
		UPD_CARMILEAGE(state,data){
			state.carMileage = data;
			localStorage.setItem('carMileage',JSON.stringify(data));
		},
		//更新项目数量
		UPD_MAINTENANCE_ITEM_NUM(state,data){
			state.maintenanceOrder.byItemNum = data;
		},
		//更新保养订单总价
		UPD_MAINTENANCE_ORDER_TOTAL_PRICE(state,data){
			state.maintenanceOrder.totalPrice = data;
		},
		//更新保养订单类型
		UPD_MAINTENANCE_ORDER_PLANTYPE(state,data){
			state.maintenanceOrder.planType = data;
		},
		//更新订单优惠金额
		UPD_MAINTENANCE_DISCOUNT(state,data){
			state.maintenanceOrder.discount = data;
		}
	}
})
